package com.rulin.asp.business.datacleaning.replication;

import java.util.Date;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.rulin.asp.attachment.service.AttachService;
import com.rulin.asp.business.datacleaning.entity.QuestionNew;
import com.rulin.asp.business.datacleaning.mapper.QuestionNewMapper;
import com.rulin.asp.business.entity.Question;
import com.rulin.asp.business.mapper.QuestionMapper;
import com.rulin.asp.sso.entity.UserUtils;

@Service
public class ReplicationService {

	@Autowired
	private QuestionNewMapper questionNewMapper;
	
	@Autowired
	private QuestionMapper questionMapper;
	
	@Autowired
	private AttachService attachService;
	
	@Transactional
	public RepliResponse replicateQuestion(String quesId, String major, String subject, String relatedQues) {
		RepliResponse response = new RepliResponse();
		response.setCode(RepliResponse.SUCCESS);
		
		QuestionNew questionNew = questionNewMapper.selectBySourceQuesId(quesId);
		if(questionNew == null) {
			Question question = questionMapper.selectByPrimaryKey(quesId);
			String quesDescribe = question.getQuesDescribe();
			String anwser = question.getMyAnwser();
			String replyId = question.getReplyId();
			String uploadUid = UserUtils.getUser().getId();
			
			questionNew = new QuestionNew();
			questionNew.setAnswer(anwser);
			questionNew.setCreateTime(new Date());
			String id = UUID.randomUUID().toString();
			questionNew.setId(id);
			questionNew.setMajor(major);
			questionNew.setSubject(subject);
			questionNew.setRelatedQuestion(relatedQues);
			questionNew.setQuestion(quesDescribe);
			questionNew.setUploadUid(uploadUid);
			questionNew.setSourceQuesId(quesId);
			questionNewMapper.insert(questionNew);
			String destQuesId = id + "--ques";
			attachService.copyBySourceAreaId(quesId, destQuesId);
			String destAnswerId = id + "--answer";
			attachService.copyBySourceAreaId(replyId, destAnswerId);
			
			return response;
		}else {
			response.setCode(RepliResponse.DATA_REPLI_ERROR);
			response.setMessage("此题已经拷贝，请勿重复复制。");
			return response;
		}
	}
}
